package com.boyuan.huijiaoyu;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import cn.jpush.android.api.JPushInterface;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.helper.DialogHelper;
import com.magicsoft.app.helper.ScreenManager;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

public abstract class BaseActivity extends Activity {
	// 加载中...显示框
	private Dialog loading;
	// 按返回键退出，默认为不允许退出，若允许退出则在backClicked()中将isBackAllowed = true
	public Boolean isBackAllowed = false;
	// 是否后台进入前台
	public boolean isBackIntoTheFrontDesk = false;
	protected ImageLoader imageLoader = ImageLoader.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ScreenManager.getScreenManager().pushActivity(this);
		if (this.getClass() == ImageViewZoomActivity.class) {
			overridePendingTransition(R.anim.activity_noanim,
					R.anim.activity_noanim);
		} else if (this.getClass() == AccountLoginActivity.class
				|| this.getClass() == SelectFriendFromContactsActivity.class) {
			overridePendingTransition(R.anim.activity_pushtotop,
					R.anim.activity_noanim);
		} else {
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		}

		if (ColorHolderApplication.getInstance().mainActivity != null) {

			isBackIntoTheFrontDesk = false;

		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);

		if (ColorHolderApplication.getInstance().mainActivity != null) {

			if (SharePreferenceHelper.IsLogin(getApplicationContext()) == false) {
				return;
			}
			ColorHolderApplication.getInstance().mainActivity.isBackIntoTheFrontDesk = isBackIntoTheFrontDesk;

//			ColorHolderApplication.getInstance().mainActivity.backendBecomeActive();
		}

	}

	/**
	 * 显示loading
	 * 
	 * @param msg
	 */
	public void showLoading(String msg) {
		if (!isFinishing()) {// Activity是否还存在

			if (loading == null) {

				loading = DialogHelper.createLoadingDialog(BaseActivity.this,
						msg);

				loading.setOnKeyListener(new OnKeyListener() {

					@Override
					public boolean onKey(DialogInterface dialog, int keyCode,
							KeyEvent event) {
						if (keyCode == KeyEvent.KEYCODE_BACK
								&& event.getRepeatCount() == 0) {

							onBackPressed();

						}
						return false;
					}
				});

			}
			loading.show();
		}
	}

	/**
	 * 隐藏loading
	 */
	public void hideLoading() {

		if (loading != null) {

			if (loading.isShowing()) {
				loading.dismiss();
				loading = null;
			}

		}

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (ColorHolderApplication.getInstance().mainActivity != null) {

			if (!ColorHolderApplication.getInstance().mainActivity
					.isAppOnForeground()) {

				isBackIntoTheFrontDesk = true;

			}
		}

	}

	/**
	 * 是否正在loading
	 * 
	 * @return
	 */
	public Boolean isShowingLoading() {

		if (loading != null) {
			return loading.isShowing();
		}

		return false;

	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		ScreenManager.getScreenManager().removeActivity(this);
		super.finish();
		if (this.getClass() == ImageViewZoomActivity.class) {
			overridePendingTransition(R.anim.activity_noanim,
					R.anim.activity_noanim);
		} else if (this.getClass() == AccountLoginActivity.class
				|| this.getClass() == SelectFriendFromContactsActivity.class) {
			overridePendingTransition(0, R.anim.activity_pushtobottom);
		} else {
			overridePendingTransition(R.anim.push_right_in,
					R.anim.push_right_out);
		}

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(this);
	}

	/**
	 * 返回事件
	 */
	public abstract void backClicked();

	@Override
	public void onBackPressed() {

		backClicked();
		if (isBackAllowed) {
			super.onBackPressed();
		}

	}

}
